/**
 * Created by dale on 2017/8/4.
 */
var keyId;
var  tempList;
var iDepId;
$(document).ready(function () {
    tempList= $('#tempList');
    $('.xh-datagrid-toolbar').parseToolbar();

    //设置禁用
    $('#bt_list_del').setButtonDisable('eu');
    $('#bt_list_manage').setButtonDisable('eu');
    $('#bt_list_upload').setButtonDisable('eu');


    tempList.treegrid({
        onSelect:function (row) {
            keyId=row['keyId'];
            tempList.treegrid('expand',keyId);
            $('#bt_list_del').setButtonEnable('eu');
            $('#bt_list_manage').setButtonEnable('eu');
            $('#bt_list_upload').setButtonEnable('eu');
        },
        onBeforeSelect:function (row) {
            if(keyId==undefined){
                return true;
            }else {
                if(keyId==row['keyId']){
                    tempList.treegrid('unselect',keyId);
                    tempList.treegrid('collapse',keyId);
                    keyId=null;
                    $('#bt_list_del').setButtonDisable('eu');
                    $('#bt_list_manage').setButtonDisable('eu');
                    $('#bt_list_upload').setButtonDisable('eu');
                    return false;
                }
            }
        },
        onBeforeCheck:function (row) {
            if(keyId==undefined){
                return true;
            }else {
                if(keyId==row['keyId']){
                    tempList.treegrid('uncheckRow',keyId);
                    keyId=null;
                    $('#bt_list_del').setButtonDisable('eu');
                    $('#bt_list_manage').setButtonDisable('eu');
                    $('#bt_list_upload').setButtonDisable('eu');
                    return false;
                }
            }
        },

        onDblClickRow:function (row) {
            var $tempId=row['realId'];
            var $name=row['name'];
            if(row['type']==1){
                window.top.xhTop.showModal({
                    title:'新增节点',
                    content: '<iframe src="'+modelUrl+'template/nodeForm?iTPId='+$tempId+'&name='+escape($name)+'"  style="width: 100%;;border-style: none;padding: 0px;height: 220px"  scrolling="yes"   ></iframe>',
                    button:'提交',
                    backClose: false,
                });
            }else {
                window.top.xhTop.showModal({
                    title:'编辑节点',
                    content: '<iframe src="'+modelUrl+'template/nodeForm?iNodeId='+$tempId+'"  style="width: 100%;;border-style: none;padding: 0px;height: 320px"  scrolling="yes"   ></iframe>',
                    button:'提交',
                    backClose: false,
                });
                
            }

        }

    });

    $('#bt_list_add').click(function () {

        var obj=tempList.treegrid('getSelections')[0];

        if(obj == undefined){
            window.top.xhTop.showModal({
                title:'新增模板',
                content: '<iframe src="'+modelUrl+'template/tempForm"  style="width: 100%;;border-style: none;padding: 0px;height: 50px"  scrolling="yes"   ></iframe>',
                button:'提交',
                backClose: false,
            });
        }else {
            var $tempId=obj['realId'];
            var $name=obj['name'];
            if(obj['type']==1){
                window.top.xhTop.showModal({
                    title:'新增节点',
                    content: '<iframe src="'+modelUrl+'template/nodeForm?iTPId='+$tempId+'&name='+escape($name)+'"  style="width: 100%;;border-style: none;padding: 0px;height: 320px"  scrolling="yes"   ></iframe>',
                    button:'提交',
                    backClose: false,
                });
            }else {
                window.top.swal(
                    '错误!',
                    '无法新增子节点',
                    'error'
                );
            }

        }

    });

    $('#bt_list_del').click(function () {   //删除
        if(xhExtra.buttonDisabled($(this))){
            return false;
        }
        var id=tempList.treegrid('getSelections')[0]['realId'];
        var type=tempList.treegrid('getSelections')[0]['type'];
        if(type==1){
            myWindow.initPopUp({title: '确定要删除吗？', text: '该模板存在子节点'},function () {
                // $.ajax({
                //     url:modelUrl+'template/delTemp',
                //     data:{id:id},
                //     type:'POST',
                //     success:function (data) {
                //         if(data == 'fail'){
                //             window.top.xhTop.showMessage({title: '操作失败！', type: 'error'});
                //             return false;
                //         }
                //         location.reload();
                //     },
                //     error:function (data) {
                //         console.log(data);
                //     }
                // });
                xhExtra.ajaxRequest('plan/template/delTemp',{id:id},{},function (data) {
                    if(data == 'fail'){
                        window.top.xhTop.showMessage({title: '操作失败！', type: 'error'});
                        return false;
                    }
                    location.reload();
                })
            })
        }else {
            myWindow.initPopUp({title: '确认删除', text: '你确定要删除该节点'},function () {
                $.ajax({
                    url:modelUrl+'template/delNode',
                    data:{id:id},
                    type:'POST',
                    success:function (data) {
                        if(data == 'fail'){
                            window.top.xhTop.showMessage({title: '操作失败！', type: 'error'});
                            return false;
                        }
                        location.reload();
                    },
                    error:function (data) {
                        console.log(data);
                    }
                });
            })
        }

    });

    $('#bt_list_upload').click(function () {
        if(xhExtra.buttonDisabled($(this))){
            return false;
        }
        if(tempList.treegrid('getSelections').length>1){
            window.top.swal('错误', '请选择一个节点', 'error');
            return false;
        }
        var id=tempList.treegrid('getSelections')[0]['realId'];
        var type=tempList.treegrid('getSelections')[0]['type'];
        if(type==2){
            $.ajax({
                url:modelUrl+'plan/uploadContent',
                data:{iNodeId:id},
                type:'POST',
                dataType:'json',
                success:function (data) {
                    if(data['message']=='success'){
                        window.top.swal('成功', '成功更新', 'success');
                    }else {
                        window.top.swal('错误', data['message'], 'error');
                    }
                },
                error:function (data) {
                    console.log(data);
                }
            });
        }else {
            window.top.swal('错误', '请选择一个子节点', 'error');
        }

    });

    $('#bt_list_manage').click(function () {  //编辑
        if(xhExtra.buttonDisabled($(this))){
            return false;
        }
        var id=tempList.treegrid('getSelections')[0]['realId'];
        var type=tempList.treegrid('getSelections')[0]['type'];

        if(type==1){
            window.top.xhTop.showModal({
                title:'编辑模板',
                content: '<iframe src="'+modelUrl+'template/tempForm?iTPId='+id+'"  style="width: 100%;;border-style: none;padding: 0px;height: 50px"  scrolling="yes"   ></iframe>',
                button:'提交',
                backClose: false,
            });
        }else {
            window.top.xhTop.showModal({
                title:'编辑节点',
                content: '<iframe src="'+modelUrl+'template/nodeForm?iNodeId='+id+'"  style="width: 100%;;border-style: none;padding: 0px;height: 320px"  scrolling="yes"   ></iframe>',
                button:'提交',
                backClose: false,
            });
        }

    });

    

});

